/*
 * PluginManager.cpp
 *
 *  Created on: 2011. 3. 15.
 *      Author: JeeHoon
 */
#include "api/PluginManager.h"
#include "PhoneGapForm.h"
#include "plugin/DebugConsole.h"
#include "plugin/Device.h"
#include "plugin/File.h"
#include "plugin/Accelerometer.h"
#include "plugin/CameraLauncher.h"
#include "plugin/Location.h"
#include "plugin/ChildBrowser.h"

using namespace Osp::Base;

PluginManager::PluginManager()
{
	__pServices = NULL;
	__pPhoneGapForm = NULL;
}

PluginManager::~PluginManager()
{
	if(NULL != __pServices )
	{
		__pServices->RemoveAll(true);
		delete __pServices;
		__pServices = NULL;
	}
}

result
PluginManager::Construct(PhoneGapForm * pPhoneGapForm)
{
	Osp::Base::Collection::IMapEnumerator* pMapEnum = NULL;
	result r = E_SUCCESS;

	__pPhoneGapForm = pPhoneGapForm;

	if(NULL == __pServices )
	{
		__pServices = new Osp::Base::Collection::HashMap();
		__pServices->Construct();
	}

	/* insert service */
	__pServices->Add(*(new String(L"DebugConsole")),*(new DebugConsole()));
	__pServices->Add(*(new String(L"Device")),*(new Device()));
	__pServices->Add(*(new String(L"Accelerometer")),*(new Accelerometer()));
	__pServices->Add(*(new String(L"Camera")),*(new CameraLauncher()));
	__pServices->Add(*(new String(L"File")),*(new File()));
	__pServices->Add(*(new String(L"Location")),*(new Location()));
	__pServices->Add(*(new String(L"ChildBrowserCommand")),*(new ChildBrowser()));

	if(E_SUCCESS == r)
	{
		pMapEnum = __pServices->GetMapEnumeratorN();
		if(NULL == pMapEnum)
		{
			r = E_FAILURE;
		}
	}

	while (pMapEnum->MoveNext() == E_SUCCESS)
	{
		String * pKey = (String*)pMapEnum->GetKey();
		Plugin * pPlugin = (Plugin*)pMapEnum->GetValue();

		pPlugin->Construct(this,*pKey);
	}

	if(NULL != pMapEnum)
	{
		delete pMapEnum;
		pMapEnum = NULL;
	}

	return r;
}



result
PluginManager::exec(String service,String action,String callbackId,String jsonArgs, bool async)
{
	result r = E_SUCCESS;
	Plugin * pPlugin = NULL;

	if(E_SUCCESS == r)
	{
		pPlugin = (Plugin*)__pServices->GetValue(service);
		r = GetLastResult();
	}

	if(E_SUCCESS == r)
	{
		r = pPlugin->execute(action,jsonArgs,callbackId);
	}
	else
	{
		printLog(L"Unknown service: " + service);
	}

	return r;
}

result
PluginManager::nativeReady()
{
	result r = E_SUCCESS;

	if(E_SUCCESS == r)
	{
		Device * pDevice = (Device*)__pServices->GetValue(String(L"Device"));
		if(NULL != pDevice)
		{
			r = pDevice->setDeviceInfo();
		}
		else
		{
			r = E_FAILURE;
		}
	}

	return r;
}

Osp::Base::String
PluginManager::evaluateJavascript(Osp::Base::String javascript)
{
	return __pPhoneGapForm->evaluateJavascript(javascript);
}

result
PluginManager::processRequest(Osp::Base::String url)
{
	result r = E_SUCCESS;
	String request = url;
	int index;
	String service,action,callbackId,jsonArgs,outPut;
	bool isAsync = false;

	request.SubString(6,request);
	/* Parse */
	if( request.IndexOf('?',0,index) == E_SUCCESS )
	{
		request.SubString(index,jsonArgs);
		request.SubString(0,index,request);
		if(jsonArgs.GetLength() == 1)
			jsonArgs.Clear();
		else
			jsonArgs.SubString(1,jsonArgs);
	}

	if( request.IndexOf('/',0,index) == E_SUCCESS )
	{
		request.SubString(index,callbackId);
		request.SubString(0,index,service);
		if(callbackId.GetLength() == 1)
			callbackId.Clear();
		else
			callbackId.SubString(1,callbackId);
	}
	else
	{
		service = request;
	}

	if( callbackId.EndsWith(L"/") )
	{
		callbackId.SubString(0,callbackId.GetLength()-1,callbackId);
	}

	if(service.IndexOf('.',0,index) == E_SUCCESS )
	{
		service.SubString(index,action);
		service.SubString(0,index,service);
		if(action.GetLength() == 1)
			action.Clear();
		else
			action.SubString(1,action);
	}
	else
	{
		printLog(L"Invalid Request : " + request);
	}

	if(E_SUCCESS == r)
	{
		r = exec(service,action,callbackId,jsonArgs,isAsync);
	}

	return r;
}

